﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using blackhouse.BlackApart.Web;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.Web;
using blackhouse.JavaScript.JQuery;

namespace blackhouse.Aparts.Web.Pages {
	public partial class DisplayReservation : NonCmsBasePage {

		#region Fields

		private string reservationNumber;
		private IUserApartmentsReservation reservation;

		#endregion

		#region Event Handlers

		protected override void OnPreInit(EventArgs e) {
			this.AllowSkinOverride = true;
			HttpContext.Current.Items.Add("skin", "blackhouse");

			base.OnPreInit(e);
		}

		protected override void OnInit(EventArgs e) {
			base.OnInit(e);

			this.ReadParameters();
			if (IsPostBack)
				this.ReceivePostback();
			this.SetInstance();
		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);

			this.SetPageSeo();
			this.PopulateControls();
		}

		#endregion

		#region Methods

		private void SetPageSeo() {
			PageProvider pageProvider = PageProvider.Instance();
			pageProvider.SetCanonicalUrl(UrlUtils.LocalizeUrl(Factory.Instance().GetPreFolderEvaluation(), string.Format("/pages/displayreservation.aspx?{0}={1}", UrlUtils.ParameterReservationNumber, this.reservationNumber)));
			pageProvider.SetPageDescription(GetGlobalResourceObject("Aparts", "checkStatus").ToFirstUpperString());
			pageProvider.SetPageKeywords(GetGlobalResourceObject("Aparts", "confirmBookingKeys").ToString());

			string title = GetGlobalResourceObject("Aparts", "confirmBooking4").ToFirstUpperString();
			if (this.reservation != null)
				title = string.Format(title, this.reservation.ReservationNumber);
			else title = string.Format(title, "");

			pageProvider.SetPageTitle(title + " - Aparts Bed&Breakfast");
		}

		private void ReceivePostback() {
			this.reservationNumber = Request[this.txtReservationNumber.UniqueID];
			Response.Redirect(ApartsHelper.MakeUrl(string.Format("/Pages/DisplayReservation.aspx?{0}={1}", UrlUtils.ParameterReservationNumber, this.reservationNumber), false));
		}

		private void SetInstance() {
			if (!string.IsNullOrEmpty(this.reservationNumber)) {
				Factory factory = Factory.Instance();
				this.reservation = factory.GetUserApartmentsReservation(0).FindByNumber(this.reservationNumber);
			}
		}

		private void ReadParameters() {
			this.reservationNumber = this.Request[UrlUtils.ParameterReservationNumber];
		}

		private void AddJs() {
			JQueryLibraries jql = new JQueryLibraries();
			jql.IncludeFileBasedOnJQuery("blackhouse.Aparts.Web.Scripts.displayReservation.js,blackhouse.Aparts.Web");
		}

		private void PopulateControls() {
			if (this.reservation == null) {
				this.pnlDetails.Visible = false;
				return;
			}

			string title = GetGlobalResourceObject("Aparts", "confirmBooking4").ToFirstUpperString();
			if (this.reservation != null)
				title = string.Format(title, this.reservation.ReservationNumber);
			else title = string.Format(title, "");

			this.lblReservationNumber.Text = title;
			this.txtReservationNumber.Text = this.reservation.ReservationNumber;
			this.lblArrivalDate.Text = this.reservation.ArrivalDate.ToString("f");
			this.lblDepartureDate.Text = this.reservation.DepartureDate.ToString("f");

			DateTime arrivalDate = this.reservation.ArrivalDate;
			if (arrivalDate.Hour >= 0 && arrivalDate.Hour < 14)
				arrivalDate = arrivalDate.Date.AddDays(-1);
			else arrivalDate = arrivalDate.Date;
			DateTime departureDate = this.reservation.DepartureDate.Date;
			int lengthOfStayInDays = (int)Math.Ceiling((departureDate - arrivalDate).TotalDays);
			this.lblLengthOfStay.Text = lengthOfStayInDays + " ";
			if (lengthOfStayInDays == 1)
				this.lblLengthOfStay.Text += "dzień";
			else
				this.lblLengthOfStay.Text += "dni";
			this.lblNumberOfPeople.Text = this.reservation.Reservations.Sum(r => r.NumberOfPeople).ToString();
			this.PopulateSelectedApartment(this.reservation.Reservations);
			this.cTotalPrice.Value = this.reservation.TotalPrice;
			this.lblPaymentType.Text = this.reservation.Payment.Name;
			this.lblVatInvoice.Text = GetGlobalResourceObject("Aparts", this.reservation.VatInvoice ? "yes" : "no").ToFirstUpperString();
			this.lblStatusName.Text = this.reservation.Status.Name;
			this.lblStatusDescription.Text = this.reservation.Status.Description;
		}

		private void PopulateSelectedApartment(List<IReservation> reservationItems) {
			Dictionary<IApartment, int> d = new Dictionary<IApartment, int>();
			foreach (IReservation r in reservationItems) {
				if (!d.ContainsKey(r.Apartment))
					d.Add(r.Apartment, 0);
				d[r.Apartment]++;
			}

			List<string> s = new List<string>();
			foreach (KeyValuePair<IApartment, int> kvp in d) {
				string si = kvp.Value + GetGlobalResourceObject("Aparts", "apartment").ToString();
				if (kvp.Value > 1) si += "y";
				si += " " + kvp.Key.Group.Name + " (" + kvp.Key.NumberOfPeople + " " + GetGlobalResourceObject("Aparts", "people") + ")";
				s.Add(si);
			}
			this.rptReservationApartments.DataSource = s;
			this.rptReservationApartments.DataBind();
		}

		#endregion

	}
}